package org.linlinjava.litemall.admin.web2.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req3;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req3_;
import org.linlinjava.litemall.admin.web2.system.dto.resp.Resp2;
import org.linlinjava.litemall.admin.web2.system.dto.resp.Resp3;
import org.linlinjava.litemall.core.util.R;
import org.linlinjava.litemall.core.util.Util;
import org.linlinjava.litemall.db.dao.TCouponMapper;
import org.linlinjava.litemall.db.domain.TCoupon;
import org.linlinjava.litemall.db.domain.TCouponExample;
import org.linlinjava.litemall.db.enumeration.Const;
import org.linlinjava.litemall.db.util.Str;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 系统设置
 */
@Api("系统设置-红包券设置")
@RestController
@RequestMapping("/system/coupon")
public class SystemCouponController {

    private static Logger log = LoggerFactory.getLogger(SystemCouponController.class);

    @Autowired
    private TCouponMapper couponMapper; 

    @ApiOperation(value = "列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型,直接传中文,选全部时不传", paramType = "query"),
    })
    @GetMapping("/list")
    public Object list(@RequestParam(required = false) String type) {
        List<TCoupon> coupons = couponMapper.selectByExample(
                TCouponExample.newAndCreateCriteria().andDeletedEqualTo(Boolean.FALSE).when(!Str.isEmpty(type), c -> c.andTypeEqualTo(Const.COUPON_TYPE.type(type))).example());
        List<Resp2> res = Util.aList(coupons.size());
        coupons.forEach(c -> {
            Resp2 dto = new Resp2();
            dto.setId(c.getId());
            dto.set_1(c.getCategory() == 0 ? "抽奖红包" : "优惠券");
            dto.set_2(Const.COUPON_TYPE.value(c.getType()));
            dto.set_3(c.getAmount().toString());
            dto.set_4(c.getMin() <= 0 ? "无门槛" : "满" + c.getMin() + "元可用");
            dto.set_5(c.getBeginTime().toLocalDate().toString() + "至" + c.getEndTime().toLocalDate().toString());
            dto.set_6(c.getExpireTime() + "天内");
            dto.set_7(c.getOpen() ? "是" : "否");
            res.add(dto);
        });
        return R.ok(res);
    }

    @ApiOperation(value = "删除红包/券")
    @PostMapping("/del")
    public Object del(@RequestBody Req3_ req) {
        log.info("删除红包/券,参数:[{}]", req);
        Integer id = req.getId();
        if (id != null) {
            if (0 <= id && id <= 8) {
                return R.fail("删除失败，抽奖项无法删除！");
            }
            TCoupon entity = couponMapper.selectByPrimaryKey(id);
            entity.setDeleted(Boolean.TRUE);
            entity.setUpdateTime(LocalDateTime.now());
            couponMapper.updateByPrimaryKey(entity);
        }
        return R.ok("删除成功!");
    }


    @ApiOperation(value = "保存红包/券", notes = "携带id过来则是修改保存，时间格式为：yyyy-MM-dd")
    @PostMapping("/add")
    public Object add(@RequestBody Req3 req) {
        log.info("保存红包/券,参数:[{}]", req);
        TCoupon entity = new TCoupon();
        if (req.getId() != null) {
            entity = couponMapper.selectByPrimaryKey(req.getId());
        } else {
            entity.setCreatedTime(LocalDateTime.now());
        }
        entity.setUpdateTime(LocalDateTime.now());
        entity.setCategory(1);
        if ("红包".equals(req.getCatalog())) {
            entity.setCategory(0);
        }
        entity.setDeleted(Boolean.FALSE);
        entity.setType(Const.COUPON_TYPE.type(req.getType()));
        entity.setAmount(req.getAmount());
        entity.setMin(req.getMin());
        entity.setBeginTime(LocalDate.parse(req.getBeginTime()).atStartOfDay());
        entity.setEndTime(LocalDate.parse(req.getEndTime()).atStartOfDay());
        entity.setExpireTime(req.getExpireTime());
        entity.setOpen(req.getEnable());
        if (req.getId() != null)
            couponMapper.updateByPrimaryKey(entity);
        else
            couponMapper.insert(entity);
        return R.ok();
    }


    @ApiOperation(value = "详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query"),
    })
    @GetMapping("/get")
    public Object get(@RequestParam Integer id) {
        TCoupon tCoupon = couponMapper.selectByPrimaryKey(id);
        return R.ok(new Resp3(tCoupon));
    }
}
